package servlet.notice;

import com.alibaba.fastjson.JSONObject;
import utils.JdbcUtil;
import utils.ResultData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet(urlPatterns = "/noticeDelete")
public class NoticeDelete extends HttpServlet {
    JdbcUtil jdbcUtil=new JdbcUtil();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.valueOf(req.getParameter("id"));
        //String name=String.valueOf(req.getParameter("name"));
        Connection connection = jdbcUtil.getConnection();
        String sql="delete from notice where id=?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            //preparedStatement.setInt(1, Integer.parseInt(name));
            preparedStatement.setInt(1,id);
            int i = preparedStatement.executeUpdate();
            ResultData resultData=null;
            if (i>0){
                resultData = ResultData.success();
            }else{
                resultData = ResultData.fail();
            }
            Object o = JSONObject.toJSON(resultData);
            resp.getWriter().print(o.toString());

        }catch (SQLException e){
            e.printStackTrace();
        }

    }
}
